<template>
  <div id="options">
    <top-bar :title="title" v-show="IsTopbar"></top-bar>
    <div class="set_list" v-if="setOne">
      <md-field>
        <md-input-item ref="input1" v-model="userPhone" readonly>
          <md-icon name="drjc_telephone" slot="left"></md-icon>
        </md-input-item>
      </md-field>
      <p class="rzBtn" @click="rzBtn">下一步</p>
    </div>
    <div v-else-if="setTwo">
      <p class="set_text">验证码已发送至手机号{{userPhone}}</p>
      <md-field>
        <md-input-item ref="yzmcode" v-model="yzm" type="digit" placeholder="请输入短信验证码">
          <md-icon name="yzm_icon" slot="left"></md-icon>
          <span class="get-code-button" slot="right" @click="getcodeBtn">{{codeText}}</span>
        </md-input-item>
      </md-field>
      <p class="rzBtn" @click="setBtn">下一步</p>
    </div>
    <div class="set_list" v-else>
      <md-field>
        <md-input-item ref="pwdcode" v-model="pwd" placeholder="请输入新密码" :type="typestatus">
          <md-icon name="mima_icon" slot="left"></md-icon>
          <md-icon :name="yincangIcon" slot="right" @click="offBtn"></md-icon>
        </md-input-item>
      </md-field>
      <p class="pwd_text">密码为6～12位数字及字母的组合，区分大小写</p>
      <p class="rzBtn" @click="setPwdSubBtn">提交</p>
    </div>
  </div>
</template>
<script>
import '@/assets/svgs/drjc_telephone.svg';
import '@/assets/svgs/yzm_icon.svg';
import '@/assets/svgs/mima_icon.svg';
import '@/assets/svgs/yincang_icon.svg';
import '@/assets/svgs/xianshi_icon.svg';
import { Tabs, Icon, Field, InputItem, Toast, Dialog } from 'mand-mobile';
import TopBar from '@/components/TopBar';
import Loading from '@/components/loading';
import { decryptByDESModeEBC, encryptByDESModeEBC } from '@/common/encrypt';
import Utils from '@/common/pakjUtils';
import http from '@/common/http';
var CryptoJS = require('crypto-js');
export default {
  components: {
    [Tabs.name]: Tabs,
    [Icon.name]: Icon,
    [TopBar.name]: TopBar,
    [Field.name]: Field,
    [Toast.name]: Toast,
    [InputItem.name]: InputItem
  },
  data() {
    return {
      title: '修改登录密码',
      setOne: true,
      setTwo: false,
      userPhone: '',
      yzm: '',
      pwd: '',
      Isoff: false,
      typestatus: 'password',
      yincangIcon: 'yincang_icon',
      countingDown: '',
      codeText: '获取验证码',
      intervalId: '',
      sec: 60,
      IsTopbar: true
    };
  },
  created() {
    if (Utils.isApp()) {
      // 判断当前环境是否在app
      this.IsTopbar = false;
    }
    this.userPhone = this.startFun(sessionStorage['web_user_phone'], 3, 4);
  },
  methods: {
    // 验证码自动聚焦
    changfouce() {
      this.$nextTick(x => {
        this.$refs.yzmcode.focus();
      });
    },
    // 登录密码自动聚焦
    pwdfouce() {
      this.$nextTick(x => {
        this.$refs.pwdcode.focus();
      });
    },
    // 下一步
    rzBtn() {
      this.getCode(false);
    },
    // 获取验证码
    getCode(type) {
      var data = {
        user_phone: encryptByDESModeEBC(sessionStorage['web_user_phone'])
      };
      Loading.show();
      http.post('pakj/common/getMobileCode', data).then(res => {
        Loading.hide();
        console.log(res.data);
        if (res.data.code === '000000') {
          Toast({
            content: '验证码已发送',
            duration: 3000,
            hasMask: true
          });
          if (type === true) {
            this.countdown();
          } else {
            this.countdown();
            this.setOne = false;
            this.setTwo = true;
          }
          this.changfouce();
        } else if (res.data.code === '404405') {
          // 未使用短信
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定',
            onConfirm: () => {
              this.setOne = false;
              this.setTwo = true;
              this.changfouce();
            }
          });
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    },
    countdown() {
      this.intervalId = setInterval(() => {
        this.countingDown = true;
        this.codeText = this.sec + '秒后重试';
        this.sec = this.sec - 1;
        if (this.sec < 0) {
          clearInterval(this.intervalId);
          this.codeText = '获取验证码';
          this.countingDown = false;
          this.sec = 60;
        }
      }, 1000);
    },
    // 重新获取验证码
    getcodeBtn() {
      if (this.countingDown === true) {
        return false;
      }
      this.getCode(true);
    },
    setBtn() {
      if (!this.yzm) {
        Toast({
          content: '请输入验证码',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      if (this.yzm.length !== 6) {
        Toast({
          content: '请输入6位有效验证码',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      // 验证验证码是否正确
      var data = {
        user_phone: encryptByDESModeEBC(sessionStorage['web_user_phone']),
        mobileCode: this.yzm
      };
      Loading.show();
      http.post('/pakj/passenger/mobileCodeValidate', data).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          this.setOne = false;
          this.setTwo = false;
          this.pwdfouce();
        } else {
          Toast({
            content: res.data.msg,
            duration: 3000,
            hasMask: true
          });
        }
      });
    },
    // 密码输入是否隐藏
    offBtn() {
      if (this.Isoff) {
        this.typestatus = 'password';
        this.yincangIcon = 'yincang_icon';
        this.Isoff = false;
      } else {
        this.typestatus = 'text';
        this.yincangIcon = 'xianshi_icon';
        this.Isoff = true;
      }
    },
    setPwdSubBtn() {
      // 确认提交
      // var reg = /[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/;
      var reg = /^(?!([a-zA-Z]+|\d+)$)[a-zA-Z\d]{6,12}$/;
      if (!this.pwd) {
        Toast({
          content: '请输入登录密码',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      if (this.pwd.length < 6 || this.pwd.length > 12) {
        Toast({
          content: '为了您的账户安全，密码须为6-12位数字与字母的组合',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      if (!reg.test(this.pwd)) {
        Toast({
          content: '为了您的账户安全，密码须为6-12位数字与字母的组合',
          duration: 3000,
          hasMask: true
        });
        return false;
      }
      var data = {
        user_phone: encryptByDESModeEBC(sessionStorage['web_user_phone']),
        user_pwd: CryptoJS.MD5(this.pwd).toString()
      };
      Loading.show();
      http.post('/pakj/passenger/retrievePwd', data).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          Dialog.alert({
            title: '提示',
            content: '修改登录密码成功',
            confirmText: '确定',
            onConfirm: () => {
              if (Utils.isApp()) {
                // 判断当前环境是否在app
                /* eslint-disable */
                var os = Utils.getOSName();
                if ((os === 'Linux' || os === 'Android') && handler) {
                  handler.pageOut();
                } else if ((os === 'iOS' || os === 'MacOSX') && window.webkit) {
                  window.webkit.messageHandlers.pageOut.postMessage(JSON.stringify(''));
                }
              } else {
                this.$router.push({
                  name: 'AccountSecurity'
                });
              }
            }
          });
        } else {
          Dialog.alert({
            title: '提示',
            content: res.data.msg,
            confirmText: '确定'
          });
        }
      });
    },
    startFun(str, frontLen, endLen) {
      var len = str.length - frontLen - endLen;
      var xing = '';
      for (var i = 0; i < len; i++) {
        xing += '*';
      }
      return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
    }
  }
};
</script>
<style lang="stylus" scoped>
p {
  display: block;
  margin-block-start: 0px;
  margin-block-end: 0px;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
}

#options {
  height: 100vh;
  background-color: color-primary-background;
}

.set_list {
  padding-top: 20px;
}

.set_text {
  text-align: center;
  padding: 42px 0px;
  font-size: 26px;
  color: #999;
}

.rzBtn {
  height: 98px;
  line-height: 98px;
  font-size: 36px;
  text-align: center;
  background-color: #FF6917;
  color: #fff;
  margin: 140px 20px 0px 20px;
  border-radius: 7px;
}

.get-code-button {
  min-width: 160px;
  height: 48px;
  font-size: 24px;
  margin-right: 90px;
  display: inline-block;
  background-color: #FF6917;
  text-align: center;
  line-height: 48px;
  color: #fff;
  border-radius: 7px;
}

.pwd_text {
  font-size: 24px;
  color: #999;
  margin-left: 30px;
  margin-top: 10px;
}
</style>
